package sealte.wdgf.system.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sealte.wdgf.model.system.SessionMapObject;

public class SessionDetailStatisticsListener implements HttpSessionListener {
	
	private static Logger logger = LoggerFactory.getLogger(SessionDetailStatisticsListener.class);
	
	@Override
	public void sessionCreated(HttpSessionEvent hse) {
		HttpSession session = hse.getSession();
		ServletContext application = session.getServletContext();
		String sessionId = session.getId();
		if(session.isNew()){
			SessionMapObject online = (SessionMapObject)application.getAttribute("SessionMap");
			if(null==online){
				online = new SessionMapObject();
			}
			online.add(sessionId, "Visitor");
			logger.info("sessionId:"+sessionId+" 被存入SessionMap对象中,");
		}
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent hse) {
		HttpSession session = hse.getSession();
		ServletContext application = session.getServletContext();
		String sessionId = session.getId();
		SessionMapObject online = (SessionMapObject)application.getAttribute("SessionMap");
		if(null!=online){
			online.remove(sessionId);
			logger.info("sessionId:"+sessionId+" 被从SessionMap对象中移除");
		}
	}

}
